package com.aatattoo.light_food_service.utils;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
    public static String ALL_TIME = "yyyy-MM-dd HH:mm:ss";
    /**
     * 将时间戳转换为日期时间
     *
     * @param s
     * @return
     */
    public static String stampToDate(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 获取当前系统时间
     *
     * @return
     */
    public static String systemTime(){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        return dateNowStr;
    }
    /**
     * 获取两个时间相差的分钟数
     * @param oldTime
     * @param newTime
     * @return
     * @throws ParseException
     */
    public static long getTimeDifference(String oldTime,String newTime) throws ParseException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime =df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff=(NTime-OTime)/1000/60;
        return diff;
    }


    public static Timestamp dateToTimestamp(Date date){
        Timestamp createTime = new Timestamp(new Date().getTime());
        return createTime;
    }

    public static Date timestampToDate(Timestamp timestamp){
        Timestamp t = new Timestamp(System.currentTimeMillis());
        Date d = new Date(t.getTime());
        return d;
    }

    /**
     * 格式化Timestamp时间格式
     * @param timestamp
     * @return
     */
    public static String  timestampFormat(Timestamp timestamp){
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
        return format;
    }

    public static Timestamp stringToTimestamp(String time){
        DateFormat dateFormat = new SimpleDateFormat(ALL_TIME);
        Timestamp timestamp = Timestamp.valueOf(time);
        return timestamp;
    }
    /*
     * 时间转时间戳
     * */
    public static long toStamp(String format, String time) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime() / 1000;
    }

    public static boolean startTimeGtendTime(String startTime, String endTime) {
        return startTimeGtendTime(startTime, endTime, ALL_TIME);
    }

    public static boolean startTimeGtendTime(String startTime, String endTime, String format) {
        return toStamp(format, startTime) > toStamp(format, endTime);
    }

}
